<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
         pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">

<%@ page import="jhu.profiteer.data.Account"%>
<%@ page import="jhu.profiteer.data.Airplane"%>
<%@ page import="jhu.profiteer.data.Location"%>
<%@ page import="java.util.Vector"%>
<%@ page import="java.util.List"%>
<%@ page import="jhu.profiteer.data.CharacterJpaController"%>
<%@ page import="jhu.profiteer.data.AirplaneJpaController"%>
<%@ page import="jhu.profiteer.data.LocationJpaController"%>

<html>
  <%
    String pageTitle = "Airplane Information";
    String cssFile = "/profiteer-game/css/profiteer.css";
  %>
  <%@ include file="/WEB-INF/jspf/admin/header.jspf" %>
  <body>
    <%@ include file="/WEB-INF/jspf/title.jspf" %>
    <div class="contentArea Body">
      <div class="pagetitle">Map</div>
      <div class="pagebody">
        <table>
          <tr>
            <td id="menu"><%@ include file="/WEB-INF/jspf/menu.jspf" %></td>
            <td>
              <div class="divround">
                <%@ include file="/WEB-INF/jspf/admin/notice.jspf" %>
                <%
                            session = request.getSession();
                            notices = (Vector<String>) session.getAttribute("notices");

                            //check for a session attribute named “character” of type Character.
                            jhu.profiteer.data.Character character = (jhu.profiteer.data.Character) session.getAttribute("character");
                            /* TEST DATA
                            if (character == null) {
                            character = new jhu.profiteer.data.Character();
                            character.setAccountId(Long.valueOf(1));
                            character.setCurrentLocationId(Long.valueOf(1));
                            character.setId(Long.valueOf(1));
                            character.setMoney(Long.valueOf(1000));
                            character.setName("PLAYER1");
                            character.setDescription("TRAVELLER");
                            }*/
                            //If no Character object is found in the session
                            if (null == character) {

                                //check for a session attribute named “account” of type Account
                                Account account = (Account) session.getAttribute("account");
                                /* If no Account object is found in the session
                                 *  redirect the user to the Index page
                                 */
                                if (null == account) {
                                    String url = "/index";
                                    RequestDispatcher dispatcher = this.getServletContext().getRequestDispatcher(url);
                                    dispatcher.forward(request, response);
                                } else {
                                    // search the database for a Character with an accountId equal to the id field of the Account object
                                    CharacterJpaController cjc = new CharacterJpaController();
                                    character = cjc.findCharacter(Long.parseLong(account.getId().toString()));

                                    //If character is found
                                    if (null == character) {
                                        String url = "/characterInformatioin.jsp";
                                        RequestDispatcher dispatcher = this.getServletContext().getRequestDispatcher(url);
                                        dispatcher.forward(request, response);
                                    }
                                }
                            }

                            if (character != null) {
                                //************ code omitted *************
                %>
                <p><img src="http://hsc.csu.edu.au/geography/urban/cities/worldcities/worldcitiesmap.gif"
                        HEIGHT="400" WIDTH="600" BORDER="2">
                </p>
                <table rules="rows" width='600'>
                    <%
                                            //search the database for the Location with an id equal to the value of the currentLocationId in the Character object
                                            LocationJpaController ljc = new LocationJpaController();
                                            Location currLocation = null;


                                            currLocation = ljc.findLocation(character.getCurrentLocationId());

                                            //retrieve all Locations from the database.
                                            List<Location> locations = ljc.findLocationEntities();
                                            for (int i = 0; i < locations.size(); i++) {
                                                Location l = (Location) locations.get(i);
                                                if (l.getId().intValue() == character.getCurrentLocationId().intValue()) {
                    %>
                    <tr>
                        <td><%=l.getName()%> (Current Location)</td>
                        <%      } else {

                                                                    if (currLocation.canTravel(character, l)) {

                        %>
                        <td><a href="/profiteer-game/travel?locationId=<%=l.getId()%>" ><%=l.getName()%></a></td>
                        <%          } else {%>
                        <td><%=l.getName()%></td>
                        <%

                                                                    }
                                                                }
                                                                // create a new row after four locations
                                                                if ((i + 1) % 4 == 0) {
                        %>
                    </tr>
                    <%                                    }
                                            }

                    %>
                    <tr/>
                </table>
                <%      }%>
              </div>
            </td>
          </tr>
        </table>
      </div>
    </div>
    <%@ include file="/WEB-INF/jspf/footer.jspf" %>
  </body>
</html>